home *** CD-ROM | disk | FTP | other *** search
- u
- BYTES: SPEEDING UP BASIC
-
- by Maurice Jones
-
-
- I am interested in efficient
- BASIC. My idea is to promote efficient
- modeling, to keep the code short, and
- to save time doing work while the user
- reads opening screens and the like.
- Considerations of what time intervals
- were needed for each task led me to
- consider questions of efficiency which
- depend on the way the machine works.
-
- There was considerable attention
- to this sort of thing in the
- literature about the 64, but it is
- riddled with half-truths and full-
- blown falsehoods. As late as the
- summer of '88, an editor of a print
- magazine reported a bug in the 64. The
- "bug" was simply his ignorance of some
- very fundamental principles of
- computing.
-
- Now let me say that I am NOT an
- expert; I am a student. What I did was
- study the question and conduct a LOT
- of [experiments]. I examined a LOT of
- code and read from the West and Leemon
- books. These are things you too can
- do.
-
-
- I began my investigation by
- designing a short program to test the
- validity of such statements as
- "Multiply is faster than divide" and
- "Variables are faster than numbers."
-
- A funny thing happened on the way
- to enlightenment. It appears that some
- of these statements are wrong and
- others need qualifications. As is
- usual in life, the questions are not
- as easily answered as we would like.
-
- In order to have any chance of
- understanding, we must examine the
- details of how a BASIC program is
- stored and executed by the 64.
-
- On power-up the computer reserves
- memory from 2048 through 40959 for
- BASIC. The code is stored first,
- starting at 2049 and continuing as far
- as needed. When the program is RUN,
- the computer reads the first line of
- code and acts on it. If the code
- causes a simple variable to be stored,
- the information is stored in the first
- seven bytes following the end of the
- code.
-
- As each new line is executed,
- additional simple variables are stored
- IN THE ORDER THEY ARE ENCOUNTERED.
- These new variables will not overwrite
- the previous variable, but each will
- use the next seven bytes. In fact,
- once a variable is stored it will
- remain there for the rest of the run
- unless a CLR statement is issued.
-
- The term "simple variable" is used
- here to mean any variable, string or
- numeric, which is not subscripted.
- Both the name and the value of numeric
- variables are stored in the seven
- bytes. String variables are stored in
- the same area but in a different way,
- which will be discussed next month.
-
- Each time a new array is needed,
- it is stored starting in the next
- space after the most recent variable.
- From this point on, each time a new
- variable is needed, ALL arrays will be
- moved up seven bytes to make room for
- the new variable.
-
- This fact has consequences which
- seem to have received little
- attention. It is the usual practice to
- DIM arrays in the beginning of
- programs and introduce variables only
- when needed. In much of the code I
- examined the arrays would be moved
- over fifty times.
-
- DIM statements cause variables to
- be stored, whether they be simple
- variables or array variables. We shall
- see how we can use this fact to make
- our programs more efficient.
-
- Much more discussion will
- eventually be needed as the study
- unfolds, but we are now ready to run
- the demo from the RUN IT option.
-
- The RUN IT option is a quick and
- dirty program which will allow you to
- read some information screens and run
- two demos. The demos are described by
- the information screens. Please read
- these screens carefully and run the
- demos more than once if you need to.
-
- I believe that the following
- statements are fully proved by the
- demos:
-
-
- 1. The number of variables used
- will have a significant effect on the
- speed of the program.
-
- 2. If a program contains large
- arrays (or many smaller arrays or some
- combination of these), the speed of
- the program will be significantly
- affected by variables which are
- introduced AFTER the arrays are in
- place.
-
- 3. The order in which variables are
- introduced in respect to each other
- can be significant. If the variables
- which are introduced late are heavily
- used, the speed of execution will be
- slowed.
-
- In part II next month, I will
- discuss string storage and suggest a
- few more BASIC speed techniques.
-
- Bibliography:
-
- 1. R.C. West, PROGRAMMING THE
- COMMODORE 64, COMPUTE!
- Publications, INC., Greensboro,
- N.C., 1985
-
- 2. Sheldon Leemon, MAPPING THE
- COMMODORE 64, COMPUTE!
- Publications, INC., Greensboro,
- N.C., 1984
-
- MJ
-
-
-